this.name = "Caduceus Damage Control Node";
this.author = "Modified Thargoid's repair bots for Caduceus, by Wyvern";
this.copyright = "2009";
this.description = "Nanite autorepair node for Caduceus class bioships. Simulates an AI controlled self-healing nanoforge and damage control teams to repair kit over time.";
this.version = "0.1";

this.shipLaunchedFromStation = function() {
	if (player.ship.hasEquipment("EQ_DCN")) {	
		if (this.damageControlTimer) {
			this.damageControlTimer.start();
		} else {
			this.damageControlTimer = new Timer(this, this.repairShip, 60, 300);
		}
	}
};

this.shipExitedWitchspace = function() {
	if (player.ship.hasEquipment("EQ_DCN")) {	
		if (this.damageControlTimer) {
			this.damageControlTimer.start();
		} else {
			this.damageControlTimer = new Timer(this, this.repairShip, 60, 300);
		}
	}
};

this.equipmentDamaged = this.equipmentDestroyed  = function(equipment) {
	if (equipment == "EQ_DCN") {
		if(this.damageControlTimer) {
			this.damageControlTimer.stop(); 
			delete this.damageControlTimer;
		}
		if(this.damageNodeTimer) {
			this.damageNodeTimer.start();
		} else {
			this.damageNodeTimer = new Timer(this, this.restoreNode, 0, 60);
		}
	}
};
	
this.playerWillEnterWitchspace = function() {
    if (this.damageControlTimer) {
		this.damageControlTimer.stop();
		delete this.damageControlTimer;
	}
    if (this.damageNodeTimer) {
		this.damageNodeTimer.stop();
		delete this.damageNodeTimer;
	}
};

this.shipDockedWithStation = function()
{
	if(this.damageControlTimer) {	
		this.damageControlTimer.stop(); 
		delete this.damageControlTimer;
	}		     
}

this.shipDied = function()
{
	if(this.damageControlTimer) {	
		this.damageControlTimer.stop(); 
		delete this.damageControlTimer;
	}
	if(this.damageNodeTimer) {	
		this.damageNodeTimer.stop(); 
		delete this.damageNodeTimer;
	}		     		
}

this.repairShip = function() 
	{ 
	if (0 < oolite.compareVersion("1.72")) 
		{ 
		// 1.71.x or earlier 
		this.thePlayerShip = player; 
		} 
	else 
		{ 
		// 1.72 or later 
		this.thePlayerShip = player.ship; 
		}
	this.damagedList = ["EQ_WORMHOLE_SCANNER_DAMAGED","EQ_MISJUMP_ANALYSER_DAMAGED","EQ_MILITARY_SCANNER_FILTER_DAMAGED","EQ_MILITARY_JAMMER_DAMAGED","EQ_RMB_CHAFF_LAUNCHER_DAMAGED","EQ_COMBAT_COMP_DAMAGED","EQ_UPS_IDCLEANER_DAMAGED", "EQ_UPS_UNIVERSAL_SCANNER_DAMAGED", "EQ_UPS_POLICE_SCANNER_DAMAGED", "EQ_CARGO_BAY_DAMAGED", "EQ_ECM_DAMAGED", "EQ_FUEL_SCOOPS_DAMAGED", "EQ_ESCAPE_POD_DAMAGED", "EQ_ENERGY_BOMB_DAMAGED", "EQ_ENERGY_UNIT_DAMAGED", "EQ_NAVAL_ENERGY_UNIT_DAMAGED", "EQ_DOCK_COMP_DAMAGED", "EQ_GAL_DRIVE_DAMAGED", "EQ_CLOAKING_DEVICE_DAMAGED", "EQ_PASSENGER_BERTH_DAMAGED", "EQ_FUEL_INJECTION_DAMAGED", "EQ_SCANNER_SHOW_MISSILE_TARGET_DAMAGED", "EQ_MULTI_TARGET_DAMAGED", "EQ_ADVANCED_COMPASS_DAMAGED", "EQ_ADVANCED_NAVIGATIONAL_ARRAY_DAMAGED", "EQ_TARGET_MEMORY_DAMAGED", "EQ_SHIELD_BOOSTER_DAMAGED", "EQ_NAVAL_SHIELD_BOOSTER_DAMAGED", "EQ_HEAT_SHIELD_DAMAGED", "EQ_FRAME_FUEL_COLLECTOR_DAMAGED", "EQ_FRAME_BOUNTY_SCANNER_DAMAGED", "EQ_ROCKHERMIT_SCANNER_DAMAGED", "EQ_EEU_DAMAGED", "EQ_MISSILE_ANALYSER_DAMAGED", "EQ_HYPER_RADIO_DAMAGED", "EQ_AMS_DAMAGED", "EQ_WELCOME_MAT_DAMAGED", "EQ_PLANETFALL_DAMAGED", "EQ_DUMMY" ];
	this.screenList = ["Wormhole Scanner","Misjump Analyser","Military Scanner Filtering","Military Scanner Jammer","Chaff Dispenser","Combat Computers","Pirate ID Cleaner", "Universal Scanner", "GalCop Scanner", "Large Cargo Bay", "E.C.M.", "Fuel Scoops", "Escape Pod", "Energy Bomb", "Extra Energy Enit", "Naval Energy Unit", "Docking Computers", "Galactic Hyperdrive", "Cloaking Device", "Passenger Berth", "Witchdrive Fuel Injectors", "Scanner Targeting Enhancement", "Multi-Targeting System", "Advanced Space Compass", "Advanced Navigational Array", "Target System Memory Expansion", "Shield Boosters", "Naval Shield Enhancement", "External Heat Shielding", "Fuel Collector", "Bounty Scanner", "Rock Hermit Locator", "Emergency Energy Unit", "Missile Analyser", "HyperRadio", "Anti-Missile System", "Planetary Information System", "Planetary Landing Capability", "Nothing could be" ];
	this.eqChance = [70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 100]; // percentage chance of equipment being fixed, for tech <9 chance = 100%, dropping 10% per level above 8.
	this.damListLength = this.damagedList.length - 1;
	this.screenListLength = this.screenList.length - 1;
	this.playerDamagedList = []; // reset the list for each new usage of the repair bots.
	this.playerDamagedItem = []; // array of locations within the above list.
	let eqCounter = 0 ; // reset the counter
	for(eqCounter = 0;eqCounter<=this.damListLength;eqCounter++)
		{
		this.checkItem = this.damagedList[eqCounter];
		if(this.thePlayerShip.hasEquipment(this.checkItem))
			{
			this.playerDamagedList.push(this.checkItem) // if it's broke, add it to the list.
			this.playerDamagedItem.push(eqCounter) // keep a note of where in the list it is.
			}
		}
	this.plistlength = this.playerDamagedList.length;
	if(this.plistlength == 0)
		{
		player.consoleMessage("All systems nominal.",5)
		}
	else
		{
		let damagedEquipment = Math.floor(Math.random() * this.plistlength); // pick a random element from the list...
		this.damagedItem = this.playerDamagedList[damagedEquipment]; // ...define the item...
		this.damagedItemNumber = this.playerDamagedItem[damagedEquipment]; // where the item to be repaired is in the list
		this.fixedItem = this.damagedItem.substring(0,this.damagedItem.length-8);
		this.fixedName = this.screenList[this.damagedItemNumber]; // define it's screen name
		this.fixChance = this.eqChance[this.damagedItemNumber]; // for tech <9 chance = 100%, dropping 10% per level above 8.
		this.fixTry = Math.random() * 100;
		if(this.fixTry < this.fixChance)
			{
			this.fixItem();
			}
		else
			{
			player.consoleMessage(this.fixedName + " is still offlined, Captain. We shall try again soon.",5);
			}
		}
	}

this.restoreNode = function() 
	{ 
	if (0 < oolite.compareVersion("1.72")) 
		{ 
		// 1.71.x or earlier 
		this.thePlayerShip = player; 
		} 
	else 
		{ 
		// 1.72 or later 
		this.thePlayerShip = player.ship; 
		}
	this.damagedList = ["EQ_DCN_DAMAGED", "EQ_DUMMY" ];
	this.screenList = ["Damage Control Node", "Nothing could be" ];
	this.eqChance = [75, 100]; // percentage chance of equipment being fixed, for tech <9 chance = 100%, dropping 10% per level above 8.
	this.damListLength = this.damagedList.length - 1;
	this.screenListLength = this.screenList.length - 1;
	this.playerDamagedList = []; // reset the list for each new usage of the repair bots.
	this.playerDamagedItem = []; // array of locations within the above list.
	let eqCounter = 0 ; // reset the counter
	for(eqCounter = 0;eqCounter<=this.damListLength;eqCounter++)
		{
		this.checkItem = this.damagedList[eqCounter];
		if(this.thePlayerShip.hasEquipment(this.checkItem))
			{
			this.playerDamagedList.push(this.checkItem) // if it's broke, add it to the list.
			this.playerDamagedItem.push(eqCounter) // keep a note of where in the list it is.
			}
		}
	this.plistlength = this.playerDamagedList.length;
	if(this.plistlength == 0) {
		player.consoleMessage("Damage Control Node regenerated! Repairs resuming.",5)
		if(this.damageNodeTimer) {
			this.damageNodeTimer.stop(); 
			delete this.damageNodeTimer;
		}
		if(this.damageControlTimer) {
			this.damageControlTimer.start();
		} else {
			this.damageControlTimer = new Timer(this, this.repairNode, 60, 300)
		}
	} else {
		let damagedEquipment = Math.floor(Math.random() * this.plistlength); // pick a random element from the list...
		this.damagedItem = this.playerDamagedList[damagedEquipment]; // ...define the item...
		this.damagedItemNumber = this.playerDamagedItem[damagedEquipment]; // where the item to be repaired is in the list
		this.fixedItem = this.damagedItem.substring(0,this.damagedItem.length-8);
		this.fixedName = this.screenList[this.damagedItemNumber]; // define it's screen name
		this.fixChance = this.eqChance[this.damagedItemNumber]; // for tech <9 chance = 100%, dropping 10% per level above 8.
		this.fixTry = Math.random() * 100;
		if(this.fixTry < this.fixChance)
			{
			this.fixItem();
			}
		else
			{
			player.consoleMessage(this.fixedName + " is regenerating. Stand by.",5);
			}
		}
	}

this.fixItem = function()
	{
	this.thePlayerShip.setEquipmentStatus(this.fixedItem,"EQUIPMENT_OK"); // and actually fix the thing!
	player.consoleMessage(this.fixedName + " repaired and online.", 5)
	switch(this.fixedItem) // specific OXP equipment which need rebooting after fixing, or have other issues.
		{
		case "EQ_FRAME_FUEL_COLLECTOR":
			{
			if(worldScripts["Fuel Collector"]) 
				{
				worldScripts["Fuel Collector"].shipLaunchedFromStation(); // restart the timers in it's world script
				}
			}
			break;
		case "EQ_FRAME_BOUNTY_SCANNER":			
			{
			if(worldScripts["Bounty Scanner"])
				{
				worldScripts["Bounty Scanner"].shipLaunchedFromStation(); // restart the timers in it's world script
				}
			}
			break;
		case "EQ_EEU":
			{
			if(worldScripts["Emergency Energy Unit"])
				{
				worldScripts["Emergency Energy Unit"].shipLaunchedFromStation(); // restart the timers in it's world script
				}
			}
			break;
		case "EQ_ROCKHERMIT_SCANNER": 
			{ 
			if(worldScripts["rockHermit_Locator"]) 
				{ 
				if(worldScripts["rockHermit_Locator"].version == "1.2.3")
					{
					worldScripts["rockHermit_Locator"].shipLaunchedFromStation() // use the inbuild scripting of the OXP to restart it.
					}
				else
					{
					this.beacons = system.shipsWithPrimaryRole("rockbeacof"); // list the inactive beacons (no lights or "R" on compass) 
					if(this.beacons.length > 0) 
						{ 
						for (let i=0; i<this.beacons.length;i++) 
							{ 
							this.beacons[i].setPosition(this.beacons[i].position.multiply(100)); //throw the rockbeacof into space... 
							this.beacons[i].explode(); //...and blow it up. 
							} 
						} 
					worldScripts["rockHermit_Locator"].buoy = "rockbeacon" 
					worldScripts["rockHermit_Locator"].addBuoys() // no inactive beacons, so use original code to add active beacons. 
					}
				} 
			}
			break; 
		}
	}